using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class Camera3D : MonoBehaviour
{
    [SerializeField]
    public float panSpeed = 10;
    public float rotationAmount = 2;
    public float ZoomSpeed = 15;

    private Camera m_camera;
    private GameObject m_MaterialUI;
    private GameObject m_PostProcessingUI;
    private bool isRotate = false;
    private bool islifting = false;
    // Start is called before the first frame update
    void Start()
    {
        m_camera = GetComponent<Camera>();
        Debug.Log(m_camera.fieldOfView);
        m_MaterialUI = GameObject.Find("UIControl");
        m_PostProcessingUI = GameObject.Find("UIPostProcessing");
        m_PostProcessingUI.SetActive(false);
    }

    void ChangePos(ref Vector3 pos)
    {
        Vector3 dir=transform.forward;
        if (islifting)
        {
            dir = transform.up;
        }
        if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
        {
            pos += dir * (panSpeed * Time.deltaTime);
        }
        
        if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S))
        {
            pos -= dir * (Time.deltaTime * panSpeed);
        }

        if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
        {
            pos += transform.right * (panSpeed * Time.deltaTime);
        }

        if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
        {
            pos -= transform.right * (panSpeed * Time.deltaTime);
        }
    }

    void Zoom(ref Camera camera,float delta)
    {
        if (delta < 0)
        {
            camera.fieldOfView = 
                Mathf.MoveTowards(camera.fieldOfView, 110  , 10*ZoomSpeed * Time.deltaTime);
        }
        else
        {
            camera.fieldOfView = 
                Mathf.MoveTowards(camera.fieldOfView, 30  , 10*ZoomSpeed * Time.deltaTime);
        }
    }

    void CameraReset(ref Camera camera)
    {
        camera.transform.rotation = Quaternion.Euler(0, 0, 0);
        camera.transform.position = new Vector3(0, 1, -10);
    }
    void Rotate()
    {
        if (!Input.GetMouseButton(1))
        {
            return;
        }
        float xRot = Input.GetAxis("Mouse X") * rotationAmount;
        float yRot = Input.GetAxis("Mouse Y") * rotationAmount;
        transform.Rotate(0, xRot, 0, Space.World); //Unity中摄像机随着x的变化绕Y轴转动，必须是绕世界坐标的Y轴
        transform.Rotate(-yRot, 0, 0);//Unity中摄像机随着y的变化绕X轴转动
    }
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.R))
        {
            CameraReset(ref m_camera);
            return;
        }
        var pos = transform.position;
        
        ChangePos(ref pos);
        
        if (Input.GetKeyDown(KeyCode.Q))
        {
            isRotate = !isRotate;
            Debug.Log("旋转开启"+isRotate);
            
        }
        if (Input.GetKeyDown(KeyCode.E))
        {
            islifting = !islifting;
            Debug.Log("升降开启"+islifting);
            
        }
        
        var delta=Input.mouseScrollDelta.y;
        if (delta != 0)
        {
            Zoom(ref m_camera,delta);
        }
        //右键按下
        if (isRotate)
        {
            Rotate();
        }
        transform.position = pos;
        if (Input.GetKeyDown(KeyCode.P))
        {
            bool active=m_MaterialUI.activeSelf;
            if (active)
            {
                m_PostProcessingUI.SetActive(true);
                m_MaterialUI.SetActive(false);
            }
            else
            {
                m_PostProcessingUI.SetActive(false);
                m_MaterialUI.SetActive(true);
            }

        }
    }
}
